package com.td.client.gui;

import org.newdawn.slick.Color;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;

import com.td.core.obj.TDMap;
import com.td.core.obj.tower.Tower;
import com.td.core.obj.tower.Tower1;
import com.td.core.obj.tower.Tower2;
import com.td.core.obj.tower.Tower3;
import com.td.core.obj.tower.Tower4;
import com.td.core.obj.tower.Tower5;

public class TowerRenderer implements TDMap.DrawParams {

	static Image tower1 = null;
	static Image tower2 = null;
	static Image tower3 = null;
	static Image tower4 = null;
	static Image tower5 = null;
	
	static {
		try {
			tower1 = new Image("data/images/tower2.png");
			tower2 = new Image("data/images/tower3.png");
			tower3 = new Image("data/images/tower4.png");
			tower4 = new Image("data/images/tower5.png");
			tower5 = new Image("data/images/tower1.png");
		} catch (SlickException e) {
			e.printStackTrace();
		}
	}
	
	public static void draw(int player, int sx, Tower t, Graphics g) {
		
		float tx = sx + t.getPosition().getX() * FIELD_SIZE;
		float ty = t.getPosition().getY() * FIELD_SIZE;
		float ts = t.getSize() * FIELD_SIZE;
		
		int alfa = 50;
		
		switch(player) {
		case 0:
			g.setColor(prepareColor(Color.blue, alfa));
			break;
		case 1:
			g.setColor(prepareColor(Color.green, alfa));
			break;
		case 2:
			g.setColor(prepareColor(Color.red, alfa));
			break;
		case 3:
			g.setColor(prepareColor(Color.orange, alfa));
			break;
		case 4:
			g.setColor(prepareColor(Color.yellow, alfa));
			break;
		case 5:
			g.setColor(prepareColor(Color.lightGray, alfa));
			break;
		case 6:
			g.setColor(prepareColor(Color.magenta, alfa));
			break;
		case 7:
			g.setColor(prepareColor(Color.pink, alfa));
			break;
		default:
			g.setColor(prepareColor(Color.white, alfa));
			break;
		}
		//do testowania zasiegu
		//g.drawOval(mr.getX() - t.getRange() * SCALE + SCALE, mr.getY() - t.getRange() * SCALE + SCALE, t.getRange() * 2 * SCALE, t.getRange() * 2 * SCALE);
		
		if(tx + ts > sx  && tx < sx + TDMap.Screen.X) {
			g.fillRect(tx, ty, ts, ts);
			Image img = preapareImage(t);
			img.setCenterOfRotation(15, 15);
			img.rotate(t.getRotation());
			img.draw(tx, ty);
			img.rotate(-t.getRotation());
			g.drawString(""+t.getPrice(), tx, ty);
		}
	}
	
	private static Color prepareColor(Color c, int a) {
		
		return new Color((int)c.getRed(), (int)c.getGreen(), (int)c.getBlue(), a);
	}
	
	private static Image preapareImage(Tower t) {	
		if(t instanceof Tower1) {
			return tower1;
		}
		else if (t instanceof Tower2) {
			return tower2;
		}
		else if (t instanceof Tower3) {
			return tower3;
		}
		else if (t instanceof Tower4) {
			return tower4;
		}
		else if (t instanceof Tower5) {
			return tower5;
		}
		return null;
	}
	
}